package com.zls.controller;

import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.page.Result;
import com.zls.pojo.SysUser;
import com.zls.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("sysuser")
@RestController
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @GetMapping("findAll")
    public List<SysUser> findAll(){
        List<SysUser> all = sysUserService.list();
        return all;
    }
    /**
     * 用户列表分页查询
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult page = sysUserService.findPage(queryPageBean);
        return page;
    }

    /**
     * 用户添加
     * @param sysUser
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody SysUser sysUser){
        int i = sysUserService.addSysUser(sysUser);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据id查
     * @param id
     * @return
     */
    @GetMapping("findById")
    public Result findById(@RequestParam("id") int id){
        SysUser byId = sysUserService.getById(id);
        Result result=new Result();
        if (byId!=null){
            result.setData(byId);
            result.setFlag(true);
        }else {
            result.setMessage("系统错误");
        }
        return result;
    }

    /**
     * 修改
     * @param sysUser
     * @return
     */
    @PostMapping("edit")
    public Result edit(@RequestBody SysUser sysUser){
        int i = sysUserService.editSysUser(sysUser);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    @GetMapping("delete")
    public Result delete(@RequestParam("id") int id){
        int i = sysUserService.delete(id);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
